using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
using WalkingTec.Mvvm.Core;
using HengTong.Model.Db.Article;
using UUIDNext;


namespace HengTong.ViewModel.Article.SysArticleVms
{
    public partial class SysArticleVm : CrudVm<SysArticle>
    {
        [Display(Name = "文章标签关联")] public List<string> SelectedArticleTagMappingsIds { get; set; }

        public List<SysArticleAttachment> SelectedArticleAttachmentsIds { get; set; }

        public SysArticleVm()
        {
            SetInclude(x => x.Category);
            SetInclude(x => x.ArticleTagMappings);
            SetInclude(x => x.ArticleAttachments);
        }

        protected override void InitVM()
        {
            if (Entity.HasID())
            {
                SelectedArticleTagMappingsIds = Entity.ArticleTagMappings?.Select(x => x.TagId.ToString()).ToList();
                SelectedArticleAttachmentsIds = DC.Set<SysArticleAttachment>()
                    .Where(e => e.ArticleId == Entity.ID)
                    .ToList();
            }
        }

        public override async Task DoAddAsync()
        {
            Entity.ArticleTagMappings = new List<SysArticleTagMapping>();
            if (SelectedArticleTagMappingsIds != null)
            {
                foreach (var id in SelectedArticleTagMappingsIds)
                {
                    SysArticleTagMapping middle = new SysArticleTagMapping();
                    middle.SetPropertyValue("TagId", id);
                    Entity.ArticleTagMappings.Add(middle);
                }
            }
            /*Entity.ArticleAttachments = new List<SysArticleAttachment>();
            if (SelectedArticleAttachmentsIds != null)
            {
                foreach (var item in SelectedArticleAttachmentsIds)
                {
                    item.ID = Uuid.NewDatabaseFriendly(Database.SqlServer);
                    item.ArticleId = Entity.ID;
                    Entity.ArticleAttachments.Add(item);
                }
            }*/

            Entity.ID = Uuid.NewDatabaseFriendly(Database.SqlServer);
            if (Entity is IBasePoco poco)
            {
                poco.CreateBy = Wtm.LoginUserInfo?.ITCode;
                poco.UpdateBy = Wtm.LoginUserInfo?.ITCode;
                poco.CreateTime = DateTime.Now;
                poco.UpdateTime = DateTime.Now;
            }

            await base.DoAddAsync();
            
            if (SelectedArticleAttachmentsIds.Any())
            {
                
                foreach (var item in SelectedArticleAttachmentsIds)
                {
                    item.ID = Uuid.NewDatabaseFriendly(Database.SqlServer);
                    item.ArticleId = Entity.ID;
                    item.Order = item.Order;
                }

                await DC.Set<SysArticleAttachment>().AddRangeAsync(SelectedArticleAttachmentsIds);

                await DC.SaveChangesAsync();
            }
        }

        public override async Task DoEditAsync(bool updateAllFields = false)
        {
            Entity.ArticleTagMappings = new List<SysArticleTagMapping>();
            if (SelectedArticleTagMappingsIds != null)
            {
                foreach (var item in SelectedArticleTagMappingsIds)
                {
                    SysArticleTagMapping middle = new SysArticleTagMapping();
                    middle.SetPropertyValue("TagId", item);
                    Entity.ArticleTagMappings.Add(middle);
                }
            }
            /*Entity.ArticleAttachments = new List<SysArticleAttachment>();
            if (SelectedArticleAttachmentsIds != null)
            {
                foreach (var item in SelectedArticleAttachmentsIds)
                {
                    item.ID = Uuid.NewDatabaseFriendly(Database.SqlServer);
                    item.ArticleId = Entity.ID;
                    Entity.ArticleAttachments.Add(item);
                }
            }*/
            
            if (SelectedArticleAttachmentsIds.Any())
            {
                //先删除原有的
                var exist = DC.Set<SysArticleAttachment>()
                    .Where(x => x.ArticleId == Entity.ID).ToList();
                if (exist.Any())
                {
                    DC.Set<SysArticleAttachment>().RemoveRange(exist);
                }
                foreach (var item in SelectedArticleAttachmentsIds)
                {
                    item.ID = Uuid.NewDatabaseFriendly(Database.SqlServer);
                    item.ArticleId = Entity.ID;
                    item.Order = item.Order;
                }
                await DC.Set<SysArticleAttachment>().AddRangeAsync(SelectedArticleAttachmentsIds);
                await DC.SaveChangesAsync();
            }

            if (Entity is IBasePoco poco)
            {
                poco.UpdateBy = Wtm.LoginUserInfo?.ITCode;
                poco.UpdateTime = DateTime.Now;
            }
            await base.DoEditAsync(updateAllFields);
        }
    }
}